#ifndef _MUNIX_PROC_H
#define	_MUNIX_PROC_H
/*******************************************************************************
 ** Name: proc.h                                                             **
 ** Description:              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the license.txt within              **
 ** this distribution or at http://www.munixos.net/licensing.                 **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME/license.txt.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 **                                                                           **
 ** Copyright (c) 2009-2010  Barry Gian James.                                **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/
// Portions (C) 2011 OpenHouseware, LLC.
// All Rights Reserved
#define _MUNIX_PROC_H_ID "$Id$"

#include "types.h"

#define DEFAULT_MAX_FILES_PER_PROC  5
#define DEFAULT_MAX_PROCESSES		251

// UNIX recognized nine process states
enum class ProcState
{
    UnknownProcState, UserRunning, KernelRunning, ReadyToRunInMem,
    AsleepInMem, ReadyToRunSwapped, SleepSwapped, Preempted, Created,
    Defunct
};
enum class ProcPriority
{
    Lowest, Default, Highest
};

// 3b
struct ProcessID
{
    pid_t   pid;
    pid_t   ppid;
    uid_t   uid;
};

// 9b
struct ProcOwner
{
    uid_t       uid;
    char **     argv;
    uint8_t     argc;
    char **     envp;

    fsNode *    cwd;
    uint8_t     umask;
};

// Composed of 1.Process Control Block which is itself composed of
// 1.A process identification,1.B process state info,1.Cprocess control
// information, 2.user stack, 3.private user address space, 4. kernel stack,
// 5. shared address space
class ProcCtrlBlock
{
public:
	ProcCtrlBlock();
	~ProcCtrlBlock();

private:
    // Process Identification
    ProcessID       procID;
    ProcessID       procGID;
    ProcState       procState;
    ProcPriority    procPriority;
    AddressSpace    procAddrSpace;
    ProcCtrlBlock * parent;
    ProcCtrlBlock * children;
    ProcCtrlBlock * nextProc;
    ProcCtrlBlock * prevProc;
    fsNode *        binary;         // a pointer to the executable binary
    AddressSpace *  addrSpace;		// addrspace.h
    TimeStruct      cpuTime;
    SRAMAddress     heapBase;
    size_t          heapBytes;
    uint8_t         cpuID;          // what CPU process is running on
    // Signals
    BitArray        sigCatch;       // signals proc is catching
    BitArray        sigIgnore;      // signals proc is ignoring
    BitArray        sigDeliver;     // signals to be delivered to process
    // signal handler table

    // File table
    fsNode *        files;
    // fs context

    // User Registers and Stack
    ProcOwner       owner;
    SRAMAddress     stackHead;
    size_t          stackMaxSize;
    SRAMAddress     progCtr;


    // process mem pages
    // vmem context (private addr space)

    ProcCtrlBlock * next;
};


// This may be an avl tree or hash tree
// We could index into the array using the ppid since those are guaranteed
// to be unique across the system instead of hashing.
class ProcessTable
{
public:
	ProcessTable();
	~ProcessTable();

private:
	HashTable<ProcCtrlBlock> *	table;
//  ProcCtrlBlock *		table[];
};

extern ProcCtrlBlock *  procSysInit;
extern ProcCtrlBlock *  procSysKSlab;


#endif	/* _MUNIX_PROC_H */

